package Server.service.impl;

import Server.dao.*;
import Server.entity.*;
import Server.service.CommonService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private PermissionsDao permissionsDao;
    @Autowired
    private NavigationDao navigationDao;
    @Autowired
    private RequestLogDao requestLogDao;
    @Autowired
    private RolePermissionsDao rolePermissionsDao;
    @Autowired
    private RoleNavigationDao roleNavigationDao;
    @Override
    public JSONArray GetNavigation(Users users) {

        List<RolePermissions> rolePermissions = rolePermissionsDao.findByRole(users.getRole());

        //根据角色找权限
        List<Permissions> permissions = new LinkedList<>();

        for(RolePermissions roper : rolePermissions){
            permissions.add(roper.getPermissions());
        }


        List<String> perStr = new LinkedList<>();
        for(Permissions per:permissions){
            perStr.add(per.getUuid());
        }
        List<Navigation> navigations = new LinkedList<>();

        List<RoleNavigation> roleNavigations = roleNavigationDao.findByRole(users.getRole());

        for(RoleNavigation roleNavigation : roleNavigations){
            navigations.add(roleNavigation.getNavigation());
        }
        List<Map<Object, Object>> rqNav = new LinkedList<Map<Object, Object>>();
        for(Navigation fp :navigations){
            if(fp.getStatus().equals(Navigation.Status.NO)){
                continue;
            }
            if (StringUtils.isEmpty(fp.getParentId())) {
                JSONObject par = JSONObject.fromObject(fp);
                Map<Object, Object> pars = (Map) par;
                List<Map<Object, Object>> sonNav = new LinkedList<Map<Object, Object>>();
                for (Navigation son : navigations) {
                    if(son.getStatus().equals(Navigation.Status.NO)){
                        continue;
                    }
                    if (son.getParentId() != null) {
                        if (son.getParentId().equals(fp.getUuid())) {
                            JSONObject sonJson = JSONObject.fromObject(son);
                            Map<Object, Object> sonMp = (Map) sonJson;
                            sonMp.put("id", son.getUuid());
                            sonMp.put("name", son.getName());
                            sonMp.put("icon", son.getIcon());
                            sonMp.put("url", "./vsp/" + son.getUri());
                            sonNav.add(sonMp);
                        }
                    }
                }
                pars.put("id", fp.getUuid());
                pars.put("name", fp.getName());
                if(par.get("uri")!=null){
                    pars.put("url", "./vsp/" + par.get("uri"));
                }
                pars.put("icon", fp.getIcon());
                pars.put("childList", sonNav);
                rqNav.add(pars);
            }
        }
        return JSONArray.fromObject(rqNav);
    }
}
